﻿using Controller.Enum;
using Controller.Model;
using Controller.Utils;
using Leeder.Logger;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DPS.ControllerClient
{
    /// <summary>
    /// 控制器工厂
    /// </summary>
    public class ControllerFactory
    {
        #region 变量声明

        /// <summary>
        /// 日志对象
        /// </summary>
        private LogOperate LogObj;
        /// <summary>
        /// 控制器实例
        /// </summary>
        private static ControllerFactory _factory;
        /// <summary>
        /// 控制器字典
        /// </summary>
        private ConcurrentDictionary<string, BaseController> controllerDictionary;

        #endregion 变量声明

        #region 事件定义


        #endregion 事件定义

        static ControllerFactory() {
            _factory = new ControllerFactory();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        private ControllerFactory()
        {
            LogObj = Log.GetLogger(GetType());
            controllerDictionary = new ConcurrentDictionary<string, BaseController>();
        }

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <returns></returns>
        public static ControllerFactory GetInstance() {
            return _factory;
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop() {
            if (controllerDictionary == null) return;
            foreach (BaseController item in controllerDictionary.Values) {
                item.Stop();
            }
            controllerDictionary.Clear();
        }

        /// <summary>
        /// 获取控制器
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public BaseController Get(string ip, int port, Action<TriggerLabelPacketModel> deviceButtonEvent = null,
            Action<string, string, ControllerExceptionCodeEnum> deviceExceptionEvent = null, Action<LabelStatePacketModel> deviceStateEvent = null,
            Action<ControllerStatePacketModel> controllerStateChangeEvent = null , Action<bool> controllerManagerStateEvent = null) {
            string key = port == 0 ? ip : $"{ip}:{port}";
            //string key = $"{ip}:{port}";
            if (!controllerDictionary.ContainsKey(key)) {
                BaseController controller = new BaseController(ip, port);
                controller.OnDeviceButtonEvent += deviceButtonEvent;
                controller.OnDeviceExceptionEvent += deviceExceptionEvent;
                controller.OnDeviceStateEvent += deviceStateEvent;
                controller.OnConnectionStateChangeEvent += controllerStateChangeEvent;
                controller.OnControllerManagerStateEvent += controllerManagerStateEvent;
                controller.Start();
                controllerDictionary.TryAdd(key, controller);
            }
            return controllerDictionary[key];
        }

    }
}
